package com.ruoyi.dataMain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.dto.RoleDTO;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import com.ruoyi.dataMain.domain.bo.DataConsumptionBo;
import com.ruoyi.dataMain.domain.vo.DataConsumptionVo;
import com.ruoyi.dataMain.domain.DataConsumption;
import com.ruoyi.dataMain.mapper.DataConsumptionMapper;
import com.ruoyi.dataMain.service.IDataConsumptionService;

import java.util.*;

/**
 * 消耗总表Service业务层处理
 *
 * @author linziheng
 * @date 2024-04-22
 */
@RequiredArgsConstructor
@Service
public class DataConsumptionServiceImpl implements IDataConsumptionService {

    private final DataConsumptionMapper baseMapper;
    private final SysDeptMapper sysDeptMapper;
    private final SysUserMapper sysUserMapper;

    /**
     * 查询消耗总表
     */
    @Override
    public DataConsumptionVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public List<DataConsumptionVo> export(DataConsumptionBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        String user = "";
        for (RoleDTO role : loginUser.getRoles()) {
            if(1719534201567039490L == role.getRoleId() || 1L== role.getRoleId()){
                user = "admin";
                break;
            }
        }

        SysUser uuu = sysUserMapper.selectUserById(loginUser.getUserId());
        SysDept sysDept = new SysDept();
        if(!"admin".equals(user)){
            sysDept = sysDeptMapper.findDeptByLeader(uuu.getRemark());
        }
        if(!Objects.isNull(sysDept)){
            //根据id 查询部门名称
            bo.setBusinessUnitName(sysDept.getDeptName());
        }else{
            bo.setSaleUser(uuu.getNickName());
        }

        // LambdaQueryWrapper<DataConsumption> lqw = buildQueryWrapper(bo);
        // Page<DataConsumptionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<DataConsumptionVo> result = baseMapper.selectConsumptionList(bo);
        return result;
    }

    /**
     * 查询消耗总表列表
     */
    @Override
    public TableDataInfo<DataConsumptionVo> queryPageList(DataConsumptionBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        String user = "";
        for (RoleDTO role : loginUser.getRoles()) {
            if(1719534201567039490L == role.getRoleId() || 1L== role.getRoleId()){
                user = "admin";
                break;
            }
        }

        SysUser uuu = sysUserMapper.selectUserById(loginUser.getUserId());
        SysDept sysDept = new SysDept();
        if(!"admin".equals(user)){
            sysDept = sysDeptMapper.findDeptByLeader(uuu.getRemark());
        }
        if(!Objects.isNull(sysDept)){
            //根据id 查询部门名称
            bo.setBusinessUnitName(sysDept.getDeptName());
        }else{
            bo.setSaleUser(uuu.getNickName());
        }
       // LambdaQueryWrapper<DataConsumption> lqw = buildQueryWrapper(bo);
       // Page<DataConsumptionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<DataConsumptionVo> result = baseMapper.selectPageDataConsumptionList(pageQuery.build(),bo);
        TableDataInfo<DataConsumptionVo> tableDataInfo = TableDataInfo.build(result);
        Map map = baseMapper.selectPageDataConsumptionTotal(bo);
        tableDataInfo.setTotals(map);
        return tableDataInfo;
    }

    /**
     * 查询消耗总表列表
     */
    @Override
    public List<DataConsumptionVo> queryList(DataConsumptionBo bo) {
        LambdaQueryWrapper<DataConsumption> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DataConsumption> buildQueryWrapper(DataConsumptionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DataConsumption> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getYearMouth()), DataConsumption::getYearMouth, bo.getYearMouth());
        lqw.between(params.get("beginDataTime") != null && params.get("endDataTime") != null,
            DataConsumption::getDataTime ,params.get("beginDataTime"), params.get("endDataTime"));
        lqw.eq(StringUtils.isNotBlank(bo.getAccountId()), DataConsumption::getAccountId, bo.getAccountId());
        lqw.like(StringUtils.isNotBlank(bo.getAccountName()), DataConsumption::getAccountName, bo.getAccountName());
        lqw.like(StringUtils.isNotBlank(bo.getCompanyName()), DataConsumption::getCompanyName, bo.getCompanyName());
        lqw.eq(StringUtils.isNotBlank(bo.getSignatory()), DataConsumption::getSignatory, bo.getSignatory());
        lqw.like(StringUtils.isNotBlank(bo.getSupplierName()), DataConsumption::getSupplierName, bo.getSupplierName());
        lqw.like(StringUtils.isNotBlank(bo.getCustomName()), DataConsumption::getCustomName, bo.getCustomName());
        lqw.like(StringUtils.isNotBlank(bo.getBusinessUnitName()), DataConsumption::getBusinessUnitName, bo.getBusinessUnitName());
        return lqw;
    }

    /**
     * 新增消耗总表
     */
    @Override
    public Boolean insertByBo(DataConsumptionBo bo) {
        DataConsumption add = BeanUtil.toBean(bo, DataConsumption.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改消耗总表
     */
    @Override
    public Boolean updateByBo(DataConsumptionBo bo) {
        DataConsumption update = BeanUtil.toBean(bo, DataConsumption.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DataConsumption entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除消耗总表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
